package com.azusa.ktc_friend.controller;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.azusa.ktc_friend.entity.Friend;
import com.azusa.ktc_friend.service.FriendService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;

/**
 * @Description tb_friend 控制器层
 * @author Azusa
 * @date 2020-10-12 18:26:41
*/
@RestController
@CrossOrigin
@RequestMapping("/friend")
public class FriendController{

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    /**
    * 查询全部数据
    * @return

    @RequestMapping(method= RequestMethod.GET)
    public Result findAll(){
        return new Result(StatusCode.SUCCESS,friendService.findAll());
    }

    /**
    * 根据ID查询
    * @param id ID
    * @return

    @RequestMapping(value="/{id}",method= RequestMethod.GET)
    public Result findById(@PathVariable String id){
        return new Result(StatusCode.SUCCESS,friendService.findById(id));
    }

    /**
    * 分页+多条件查询
    * @param searchMap 查询条件封装
    * @param page 页码
    * @param size 页大小
    * @return 分页结果

    @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
        public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page<Friend> pageList = friendService.findSearch(searchMap, page, size);
        return  new Result(StatusCode.SUCCESS, new PageResult<Friend>(pageList.getTotalElements(), pageList.getContent()) );
    }

    /**
    * 根据条件查询
    * @param searchMap
    * @return

    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(StatusCode.SUCCESS, friendService.findSearch(searchMap));
    }
    */

    /**
    * 关注用户
    * @param friendId
    */
    @PutMapping("like/{friendId}")
    public Result addLikeFriend(@PathVariable("friendId")String friendId){
        //String userId = (String) request.getAttribute("userId");
        String userId = "1316659051995226112";
        boolean isSuccess = friendService.addLikeFriend(new Friend(userId, friendId));
        if (!isSuccess){
            return new Result(StatusCode.SUCCESS,"取消关注成功");
        }
        return new Result(StatusCode.SUCCESS,"关注成功");
    }

    /**
     * 拉黑用户
     * @param friendId
     * @return
     */
    @DeleteMapping("/{friendId}")
    public Result deleteFriend(@PathVariable("friendId")String friendId){
        //String userId = (String) request.getAttribute("userId");
        String userId = "1316659051995226112";
        boolean isSuccess = friendService.deleteFriend(new Friend(userId, friendId));
        if (!isSuccess){
            return new Result(StatusCode.SUCCESS,"取消拉黑成功");
        }
        return new Result(StatusCode.SUCCESS,"拉黑成功");
    }

    /**
    * 修改
    * @param friend

    @RequestMapping(value="/{id}",method= RequestMethod.PUT)
    public Result update(@RequestBody  Friend  friend, @PathVariable String id ){
        friend.setUserid(id);
        friendService.update( friend);
        return new Result(StatusCode.SUCCESS);
    }

    /**
    * 删除
    * @param id

    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public Result delete(@PathVariable String id ){
        friendService.deleteById(id);
        return new Result(StatusCode.SUCCESS);
    }
    */
}
